package com.example.qincetest.utils


import android.content.res.Resources
import android.util.DisplayMetrics
import android.util.Log
import java.lang.reflect.Field
import com.example.qincetest.MyApplication

private const val DEFAULT_WIDTH = 375 //默认宽度
private const val DEFAULT_HEIGHT = 1920 //默认高度

/*
屏幕适配
* */
object AdaptScreenUtils {

    private var isInitMiui = false
    private var mTmpMetrics: Field? = null

    /**
     *  横向适配
     */
    fun adaptWidth(resources: Resources, designWidth: Int = DEFAULT_WIDTH): Resources {
        val dm = getDisplayMetrics(resources)
        dm.xdpi = dm.widthPixels * 72f / designWidth
        val newXdpi = dm.xdpi
        setAppDmXdpi(newXdpi)
        return resources
    }

    /**
     * 纵向适配
     */
    fun adaptHeight(resources: Resources, designHeight: Int = DEFAULT_HEIGHT): Resources {
        val dm = getDisplayMetrics(resources)
        dm.xdpi = dm.heightPixels * 72f / designHeight
        val newXdpi = dm.xdpi
        setAppDmXdpi(newXdpi)
        return resources
    }

    /**
     * 取消适配
     */
    fun closeAdapt(resources: Resources): Resources {
        val dm = getDisplayMetrics(resources)
        dm.xdpi = dm.density * 72
        val newXdpi = dm.xdpi
        setAppDmXdpi(newXdpi)
        return resources
    }

    /**
     *  pt的值到px的值
     *
     * @param ptValue pt的值
     * @return px的值
     */
    fun pt2Px(ptValue: Float): Int {
        val metrics = MyApplication.instance.resources.displayMetrics
        return (ptValue * metrics.xdpi / 72f + 0.5).toInt()
    }

    /**
     * px的值到pt的值。
     *
     * @param pxValue px的值
     * @return pt的值
     */
    fun px2Pt(pxValue: Float): Int {
        val metrics = MyApplication.instance.resources.displayMetrics
        return (pxValue * 72 / metrics.xdpi + 0.5).toInt()
    }

    private fun setAppDmXdpi(xdpi: Float) {
        MyApplication.instance.resources.displayMetrics.xdpi = xdpi
    }

    private fun getDisplayMetrics(resources: Resources): DisplayMetrics {
        return getMiuiTmpMetrics(resources) ?: return resources.displayMetrics
    }

    private fun getMiuiTmpMetrics(resources: Resources): DisplayMetrics? {
        if (!isInitMiui) {
            var ret: DisplayMetrics? = null
            val simpleName = resources.javaClass.simpleName
            if ("MiuiResources" == simpleName || "XResources" == simpleName) {
                try {
                    mTmpMetrics = Resources::class.java.getDeclaredField("mTmpMetrics")
                    mTmpMetrics!!.isAccessible = true
                    ret = mTmpMetrics!!.get(resources) as DisplayMetrics
                } catch (e: Exception) {
                    Log.e("AdaptScreenUtils", "no field of mTmpMetrics in resources.")
                }

            }
            isInitMiui = true
            return ret
        }
        if (mTmpMetrics == null) return null
        try {
            return mTmpMetrics!!.get(resources) as DisplayMetrics
        } catch (e: Exception) {
            return null
        }
    }
}
